package org.dfl.plugin.autoalign;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SvgParser {
	InputStream is;
	Map<String, Dimension> shifts;

	public SvgParser(String svg, Map<String, Dimension> shifts) {
		byte[] bytes;
		try {
			bytes = svg.getBytes ("UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new SvgParsingException("UnsupportedEncodingException");
		}
		this.is = new ByteArrayInputStream (bytes);
		this.shifts = shifts;
	}

	public SvgParser(InputStream is, Map<String, Dimension> shifts) {
		this.is = is;
		this.shifts = shifts;
	}
	
	public Map<String, Point> parse() {
		Map<String, Point> map = new HashMap<String, Point>();
		try {
		    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		    //domFactory.setNamespaceAware(true); // never forget this!
		    DocumentBuilder builder = domFactory.newDocumentBuilder();
		    Document doc = builder.parse(is);

		    XPathFactory factory = XPathFactory.newInstance();
		    XPath xpath = factory.newXPath();
		    Object result = xpath.evaluate("//g[@class='node']", doc, XPathConstants.NODESET);
		    
		    NodeList nodes = (NodeList) result;
		    for (int i = 0; i < nodes.getLength(); i++) {
		    	Node g = nodes.item(i);
		    	//<title> -> text
		    	String id = g.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
/*		    	//<poligon> or <ellipse>
		    	Node p_e = g.getChildNodes().item(2);
		    	if (p_e.getNodeName().equals("polygon")) {
		    		String s = p_e.getAttributes().getNamedItem("points").getNodeValue();
		    		s = s.replace(' ', ',');
		    		String[] ss = s.split(",");
		    		String cx = ss[0];
		    		String cy = ss[1];
		    		map.put(id, new Point(Integer.parseInt(cx),Integer.parseInt(cy)));
		    	} else { //(p_e.getNodeName().equals("ellipse"))
		    		String cx = p_e.getAttributes().getNamedItem("cx").getNodeValue();
		    		String cy = p_e.getAttributes().getNamedItem("cy").getNodeValue();
		    		map.put(id, new Point(Integer.parseInt(cx),Integer.parseInt(cy)));
		    	}
*/
		    	Node text = g.getChildNodes().item(4);
		    	String sx = text.getAttributes().getNamedItem("x").getNodeValue();
		    	String sy = text.getAttributes().getNamedItem("y").getNodeValue();
		    	int x = Integer.parseInt(sx) + shifts.get(id).width;
		    	int y = Integer.parseInt(sy) + shifts.get(id).height;
		    	map.put(id, new Point(x, y));
		    }
		} catch (Exception e) {
			throw new SvgParsingException(e.getLocalizedMessage());
		}

		return map;
	}
}
